package cn.mdmm.controller.news;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONPObject;

import cn.mdmm.controller.BaseController;
import cn.mdmm.entity.news.NewsInfoModel;
import cn.mdmm.entity.response.ResponseBodyToGridData;
import cn.mdmm.service.news.NewsInfoService;
import cn.mdmm.service.system.SysDictService;
import cn.mdmm.service.type.TypeInfoService;
import cn.modoumama.common.annotation.OpLog;
import cn.modoumama.page.GridDataModel;
import cn.modoumama.page.Page;

@Controller
@RequestMapping(value = "/news")
@OpLog(logDesc="内容管理")
public class NewsJsonpController extends BaseController {
	
	@Autowired
	NewsInfoService newsInfoService;
	@Autowired
	TypeInfoService typeInfoService;
	@Autowired
	protected SysDictService sysDictService;
	
    /**
	 * <p>获取新闻列表</p>  
	 * @param request
	 * @param language
	 * @param title
	 * @param type
	 * @param level
	 * @return
	 * 创建人：邓强   <br>
	 * 创建时间：2017年11月7日 下午4:43:32    <br>
	 */
	@RequestMapping(value = "list")
	@ResponseBody
	@OpLog(logDesc="获取新闻列表")
	public JSONPObject findByConditions(HttpServletRequest request, String typeEnglishName, String callback){
		ResponseBodyToGridData responseBodyToGridData = new ResponseBodyToGridData();
		Integer type = typeInfoService.getTypeId(typeEnglishName, null);
		try{
			Page po = getPage(request, "`PUBLISH_DATE` DESC");
			String language = sysDictService.getDetailValue("语言", "zh_HK");
			po.addCondition("language", language);
			po.addCondition("type", type);
			GridDataModel gridDataModel = newsInfoService.getGridDataModelByCondition(po);
			responseBodyToGridData.setData(gridDataModel);
		} catch(Exception e){
			logger.error("获取新闻列表", e);
			responseBodyToGridData.addErrInfo("999", "获取新闻列表异常", null);
		}
		JSONPObject jsonpObject = new JSONPObject(callback);
		jsonpObject.addParameter(responseBodyToGridData);
		
		return jsonpObject;
	}
	
	@RequestMapping(value = { "info"})
	@ResponseBody
	public JSONPObject info(HttpServletRequest request, String callback, Long id) {
		NewsInfoModel news = newsInfoService.getNewsInfo(id);
		JSONPObject jsonpObject = new JSONPObject(callback);
		jsonpObject.addParameter(news);
		
		return jsonpObject;
	}
}
